import { Injectable } from '@nestjs/common';
import { ResultData } from 'src/common/utils/result';
import { ExportTable } from 'src/common/utils/export';
import { Response } from 'express';
import { CreatePostDto, UpdatePostDto, ListPostDto } from './dto/index';
import { PrismaService } from '@/module/common/prisma/prisma.service';
import { pickEquals, pickLike } from '@/common/utils/prisma';
import { CacheEvict } from '@/common/decorators/redis.decorator';
import { CacheEnum } from '@/common/enum';

@Injectable()
export class PostService {
    constructor(
        private readonly prisma: PrismaService
    ) { }
    @CacheEvict(CacheEnum.SYS_USER_KEY, '*')
    async create(createPostDto: CreatePostDto) {
        await this.prisma.post.create({ data: createPostDto });
        return ResultData.ok();
    }

    async findAll(query: ListPostDto) {
        let where: any = {
            delFlag: '0',
            ...pickEquals(query, ['status']),
            ...pickLike(query, ['postName', 'postCode']),
        };
        let skip = void 0;
        let take = void 0;
        if (query.pageSize && query.pageNum) {
            skip = query.pageSize * (query.pageNum - 1);
            take = +query.pageSize;
        }

        const [list, total] = await Promise.all([
            this.prisma.post.findMany({
                where,
                skip,
                take
            }),
            this.prisma.post.count({ where }),
        ]);

        return ResultData.ok({
            list,
            total,
        });
    }

    async findOne(postId: number) {
        const res = await this.prisma.post.findUnique({
            where: {
                postId,
                delFlag: '0',
            },
        });
        return ResultData.ok(res);
    }

    async update(updatePostDto: UpdatePostDto) {
        const res = await this.prisma.post.update({
            where: {
                postId: updatePostDto.postId
            },
            data: updatePostDto
        });
        return ResultData.ok(res);
    }
    @CacheEvict(CacheEnum.SYS_USER_KEY, '*')
    async remove(postIds: string[]) {
        const data = await this.prisma.post.updateMany({
            where: {
                postId: {
                    in: postIds.map(item => +item)
                }
            },
            data: {
                delFlag: '1'
            }
        });
        await this.prisma.userPost.deleteMany({
            where: {
                postId: {
                    in: postIds.map(item => +item)
                }
            }
        });
        return ResultData.ok(data);
    }

    /**
     * 导出岗位管理数据为xlsx文件
     * @param res
     */
    async export(res: Response, body: ListPostDto) {
        delete body.pageNum;
        delete body.pageSize;
        const list = await this.findAll(body);
        const options = {
            sheetName: '岗位数据',
            data: list.data.list,
            header: [
                { title: '岗位序号', dataIndex: 'postId' },
                { title: '岗位编码', dataIndex: 'postCode' },
                { title: '岗位名称', dataIndex: 'postName' },
                { title: '岗位排序', dataIndex: 'postSort' },
                { title: '状态', dataIndex: 'status' },
            ],
        };
        ExportTable(options, res);
    }
}
